//
//  OSApplyVC.m
//  ZKPayApp
//
//  Created by 凯朱 on 2024/10/30.
//

#import "OSApplyVC.h"
#import "OSApplyView.h"

@interface OSApplyVC (){
    NSMutableDictionary *_applyInfo;
}
@property (nonatomic, strong)OSApplyView *headView;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation OSApplyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请";
    self.view.backgroundColor = kClearColor;
    [self.view addSubview:self.tableView];
    _headView = [OSApplyView viewFromXIB];
    _headView.frame = CGRectMake(0, 0, KScreenWidth,1100);
    kWeakSelf(self);
    [_headView setBtnBlock:^(NSInteger btnIndex) {
        [weakself hanldeBtnAction:btnIndex];
    }];
    self.tableView.tableHeaderView = _headView;
    NSString *money = @"";
    NSString *incomeMoney = @"";
    NSString *lixi = @"";
    NSString *backMoney = @"";
    NSInteger moneyInt;
    if (_type == 1) {
        money = @"50,000";
        incomeMoney = @"50,000";
        moneyInt = 50000;
        lixi = @"2,500";
        backMoney = @"52,500";
    }else if (_type == 2){
        money = @"5,000";
        incomeMoney = @"5,000";
        moneyInt = 5000;
        lixi = @"250";
        backMoney = @"5,250";
    }else if (_type == 3){
        money = @"80,000";
        incomeMoney = @"80,000";
        moneyInt = 80000;
        backMoney = @"84,000";
        lixi = @"4,000";
    }
    NSString *dateStr = [NSString dateStrFromDate:[[NSDate date] dateByAddingDays:91] formatStr:@"yyyy.MM.dd"];
    NSString *creatDate = [NSString dateStrFromDate:[NSDate date] formatStr:@"yyyy-MM-dd HH:mm"];
    _headView.eDuLbl.text = money;
    _headView.jinELbl.text = incomeMoney;
    _headView.dateLbl.text = dateStr;
    _headView.liXiLbl.text = lixi;
    _headView.backJinELbl.text = backMoney;
    _headView.tf2.userInteractionEnabled = NO;
    _applyInfo = [NSMutableDictionary new];
    [_applyInfo setObject:money forKey:@"money"];
    [_applyInfo setObject:incomeMoney forKey:@"incomeMoney"];
    [_applyInfo setObject:dateStr forKey:@"dateStr"];
    [_applyInfo setObject:lixi forKey:@"lixi"];
    [_applyInfo setObject:backMoney forKey:@"backMoney"];
    [_applyInfo setObject:creatDate forKey:@"creatDate"];
    [_applyInfo setObject:kSTRING(@([self generateRandomTenDigitNumber])) forKey:@"id"];
    
}

- (NSUInteger)generateRandomTenDigitNumber {
    // 生成 10 位随机数，范围为 1000000000 到 9999999999
    NSUInteger randomNumber = arc4random_uniform(9000000000) + 1000000000;
    return randomNumber;
}


-(void)hanldeBtnAction:(NSInteger)index{
    
    if (index == 1) {//提交
        if (!_headView.readBtn.isSelected) {
            kShowToast(@"请先同意借款协议");
            return;
        }
        if (_headView.tf1.text.length == 0) {
            kShowToast(@"请输入持卡人姓名");
            return;
        }
        if (_headView.tf2.text.length == 0) {
            kShowToast(@"请选择收款银行");
            return;
        }
        if (_headView.tf3.text.length == 0) {
            kShowToast(@"请输入银行卡号");
            return;
        }
        if (_headView.tf4.text.length == 0) {
            kShowToast(@"请再次输入银行卡号");
            return;
        }
        if (![_headView.tf3.text isEqualToString:_headView.tf4.text]) {
            kShowToast(@"两次银行卡号输入不一致");
            return;
        }
        
        [OSTool showHudOnWindow];
        [OSHttpManager postWithPath:@"appInfo" params:nil finished:^(BOOL isSuccessed, id result, NSString *code, NSString *message) {
            [OSTool hideHudOnKeyWindow];
            kShowToast(@"申请成功");
            NSString *key = [NSString stringWithFormat:@"curOrder_%@",kUserInfo.user_id];
            [[NSUserDefaults standardUserDefaults] setObject:_applyInfo forKey:key];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
                if (self.applyBlock) {
                    self.applyBlock(1);
                }
            });
        }];
    }
}


#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight,kScreenWidth,kScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kClearColor;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

@end
